Difference Between Query and Mutation Endpoints in RTK Query
RTK Query distinguishes between two types of endpoints — queries and mutations — based on the kind of operation being performed. Queries are used to fetch and read data, while mutations are used to modify data on the server.
Defined using builder.query().
Used for fetching and retrieving data from an API.
Results are automatically cached by RTK Query for performance.
Support features like polling, background refetching, and cache sharing.
Example hooks: useGetUsersQuery, useFetchPostsQuery.
Defined using builder.mutation().
Used for creating, updating, or deleting data on the server.
Do not automatically cache results (but can trigger cache invalidation).
Useful for actions like submitting forms or updating user profiles.
Example hooks: useAddUserMutation, useDeletePostMutation.
In short, queries are for reading data and mutations are for writing data. RTK Query handles both efficiently while providing automatic caching and refetching where appropriate.